﻿namespace System.Text
{
	internal sealed class CP866UEncoder : Encoder
	{
		private readonly Encoder _encoder = Encoding.GetEncoding(866).GetEncoder();

		public override int GetByteCount(char[] chars, int charIndex, int charCount, bool flush)
		{
			return _encoder.GetByteCount(chars, charIndex, charCount, flush);
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "3")]
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
		public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)
		{
			var count = _encoder.GetBytes(chars, charIndex, charCount, bytes, byteIndex, flush);

			for (var i = 0; i < count; i++)
			{
				switch (chars[charIndex + i])
				{
					case (char)0x490: bytes[byteIndex + i] = 0xF2; break; //Ґ
					case (char)0x491: bytes[byteIndex + i] = 0xF3; break; //ґ
					case (char)0x404: bytes[byteIndex + i] = 0xF4; break; //Є
					case (char)0x454: bytes[byteIndex + i] = 0xF5; break; //є
					case (char)0x406: bytes[byteIndex + i] = 0xF6; break; //І
					case (char)0x456: bytes[byteIndex + i] = 0xF7; break; //і
					case (char)0x407: bytes[byteIndex + i] = 0xF8; break; //Ї
					case (char)0x457: bytes[byteIndex + i] = 0xF9; break; //ї
				}
			}

			return charCount;
		}
	}
}